package com.zhaopin.gitlab;

import com.zhaopin.utils.FileUtil;
import com.zhaopin.utils.GitUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class UpdateExpThenUploadGit {

    static String projectName = "project";
    //    static String projectName = "position";
    static String outputDir = projectName.equals("project") ? "dir/gitlab/recommend" : "dir/gitlab/position";

    public static void main(String[] args) throws IOException {
        //要替换的原值
        String old = "    whereList.add(\"u.user_id==\" + id);";
        String[] splitOld = old.split("\n");
        String newValue = "    whereList.add(\"id(u)==\" + \"'\"+id+\"'\");";
        String gitFile = "/Users/zhaopin/IdeaProjects/" + projectName + "/%s";

        String proPath = "/Users/zhaopin/IdeaProjects/" + projectName + "/%s/project/src/main/java";
        List<String> projectNameList = FileUtils.readLines(new File(outputDir + "/" + projectName + "_need_update_project.txt"), Charset.defaultCharset());


        List<String> realProjectNameList = new ArrayList<>();
        for (String s : projectNameList) {
            if (s.contains(":")) {
                realProjectNameList.add(s.split(":")[0]);
            }

        }

        int i = 0;
        boolean isProjectUpdated = false;
        for (String s : realProjectNameList) {
            //获取项目的目录
            String projectPath = String.format(proPath, s);
            String gitFileNew = String.format(gitFile, s);
            File gitFileM = new File(gitFileNew);
            //首先拉取
            GitUtils.gitPull(gitFileM);

            List<File> allFiles = FileUtil.getAllFiles(projectPath);
            for (File singleFile : allFiles) {
                String oldValue = FileUtils.readFileToString(singleFile, Charset.defaultCharset());

                String replace = oldValue;
                boolean flag = false;
                for (String s1 : splitOld) {
                    String tmp = replace;
                    replace = replace.replace(s1, newValue);
                    //此处进行判断哪些文本文件是修改了的
                    if (replace.contains(newValue) && tmp.contains(s1) && !replace.equals(tmp)) {
                        flag = true;
                        isProjectUpdated = true;
                    }
                }
                if (flag) {
                    FileUtils.writeStringToFile(singleFile, replace, Charset.defaultCharset(), false);
                    //记录一下更改的文件
                    FileUtils.write(new File(outputDir + "/" + "com-files.txt"), i + ":" + singleFile.getAbsolutePath(), Charset.defaultCharset(), true);
                    FileUtils.write(new File(outputDir + "/" + "com-files.txt"), "\r\n", Charset.defaultCharset(), true);
                }
            }

            if (isProjectUpdated) {
                //记录一下更改的项目
                FileUtils.write(new File(outputDir + "/" + "com-project.txt"), i + ":" + gitFileM.getName(), Charset.defaultCharset(), true);
                FileUtils.write(new File(outputDir + "/" + "com-project.txt"), "\r\n", Charset.defaultCharset(), true);
                //进行提交项目的操作 git pull ;git add ; git commit ; git push
                //projectPath的值为目录  /Users/zhaopin/IdeaProjects/project/config-position_recommend-position_deliver-default-c_App_r_a_bizcvpromote_V2/project/src/main/java
                GitUtils.gitAllDo(gitFileM, "ng优化", null, true);

            }

            isProjectUpdated = false;
            i++;


        }

    }

}
